One family of seven bounded containers - array, list, grid, stack, queue, map and set - and how to pick the right one.
Most of the time a variable holds one value - an int, a string, a ref<obj>. A data container holds many values at once, under one name. GBML has seven of them: array<T>, list<T>, grid<T>, stack<T>, queue<T>, map<K,V>, and set<T> for "which of these have I already seen." They look like seven different things with seven different rulebooks, but they are really one family with one shared rulebook (below) and a handful of honest differences that decide which one to reach for.
Every container on this page is covered in full on its own pages - this one is the map of the family, not a replacement for them.
| Container | Shape | Reach it by | Extra hardware rule | Reach for it when |
|---|---|---|---|---|
array<T> | a fixed set of slots | a[i], or array_length/array_copy/array_sort/array_shuffle/array_foreach/array_filter | none | you already know how many, and that count never changes - a fixed inventory of five weapon slots |
list<T> | items that come and go, in order | a[i], or list_add/list_insert/list_remove/list_fill/list_create_fill/list_copy/list_contains/list_sort/list_shuffle/list_foreach/list_filter/list_keep | none | an inventory, a high-score table, "the last few things that happened" |
grid<T> | a fixed 2-D block of cells | board[# x, y], or grid_width/grid_height/grid_fill/grid_fill_region/grid_get_region_sum/grid_get_region_max/grid_get_region_mean/grid_copy/grid_sort/grid_sort_rows/grid_shuffle/grid_shuffle_rows/grid_shuffle_columns/grid_foreach/grid_filter | none | a board, a tilemap, anything naturally rows-and-columns |
stack<T> | one end only - the top | stack_push/stack_pop/stack_peek/stack_copy | none | undo history, a "back" trail - last in, first out |
queue<T> | one end in, the other end out | queue_push/queue_pop/queue_peek/queue_copy | capacity must be a power of two | turn order, a spawn queue - first in, first out |
map<K,V> | values looked up by key, not position | map_get/map_set/map_has/map_copy | capacity must be a power of two; the key must be hashable (below) | "what is alice's score" - looking things up by name |
set<T> | membership only - no value, no position | set_has/set_add/set_remove/set_copy | capacity must be a power of two; the element must be hashable (below) | "have I already visited this tile" - fast yes/no membership |
array<T>, list<T> and grid<T> are the three containers that keep an accessor at all - plain [i] for the first two, [# x, y] for the grid. Accessors covers how that syntax works across all three in one place; Arrays covers the array<T> vs list<T> choice specifically, and Grids the two-number case. stack<T>, queue<T>, map<K,V> and set<T> drop indexing entirely and are reached only through their own named functions - a deliberate trade, covered on that same Accessors page and below.
However different the seven look in the table above, they all obey the same three rules - a fact worth knowing once rather than re-reading on every single page.
/// @capacity N comment, the number inside the type (list<int, 16>), or the number in a function that makes the container (list_create(16, 0)). The functions are array_create, list_create, stack_create, queue_create, set_create, map_create and grid_create; see Writing a type out for the whole picture. Leave the size off in every one of them and you get 8. grid<T> alone needs a shape, not just a total - /// @capacity WxH (say 8x6), grid<int, 8, 6> or grid_create(8, 6, 0) - see Grids for why.false. There is one function that works the other way, list_insert: on a full list it keeps the new item and the last item is lost. The next section lines every case up. Ignore the return value and the container simply stops accepting new items; check it only when the game needs to react to "no room left."stack<T>/queue<T>, or map_get-ing a key that was never set, hands back a plain default value instead - 0, 0.0, false, an empty string, or nothing for a ref<obj>, depending on what the container holds. That is a real, ordinary value, not an error marker, so the only way to tell "the container really held this" apart from "the container was empty/the key was missing" is to ask first - stack_empty/queue_empty/map_has/ set_has exist for exactly that."What happens when it is full?" is the first question to ask of any container with a fixed size, so here is every answer in one place. The rule behind all of them is the same: whatever would end up past the end is lost, and nothing crashes. For almost every function, what is lost is the new item. For list_insert it is the last item.
| You do this to a full... | What happens | It gives back |
|---|---|---|
list, list_add(items, x) | x is not added; everything already in the list stays | false |
list, list_insert(items, i, x) with i before the end | x goes in at i; the last item falls off the end and is lost | true |
list, list_insert(items, list_length(items), x) | nothing changes - x is the one that would be past the end | false |
stack, stack_push(s, x) | x is not pushed; the stack is unchanged | false |
queue, queue_push(q, x) | x is not added; the queue is unchanged | false |
set, set_add(s, x) with a new x | x is not added; the set is unchanged | false |
set, set_add(s, x) where x is already in it | nothing to do, x is a member | true |
map, map_set(m, k, v) with a new key | the pair is not stored; the map is unchanged | false |
map, map_set(m, k, v) with a key that is already in it | the old value is replaced - a map does not grow when a value is replaced | true |
An array and a grid cannot be full in this way, because they have no add: every slot is there from the start. The box is a fixed size on purpose. A GBA has no spare memory to make it bigger while the game runs, so when there is no room something has to be left out, and these are the places that say which. If the lost item would matter, compare list_length or its twin with the size you gave the container, and only add when there is room.
Four jobs have a function for the three containers that keep their items in a row or a block - the array, the list and the grid - and for no other:
| Job | Array | List | Grid |
|---|---|---|---|
| put numbers or text in order | array_sort | list_sort | grid_sort (each column or row on its own), grid_sort_rows (whole rows, by one column) |
| mix up the order, in place | array_shuffle | list_shuffle | grid_shuffle (the cells), grid_shuffle_rows, grid_shuffle_columns |
| run one of your scripts on every item | array_foreach | list_foreach | grid_foreach |
| make a list of the items a script says yes to | array_filter | list_filter | grid_filter |
| take the items a script says no to out of the container itself | (an array has a fixed size) | list_keep | (a grid has a fixed size) |
A few rules hold for all of them:
list_sort). A sort of yes-or-no values, structs or references is refused when the project is checked. Shuffling is for anything, because nothing is compared.array_copy, list_copy or grid_copy.roll_int. So a game repeats its shuffles every time it is switched on, roll_reseed changes that, and a shuffle uses up some rolls.function (n) { ... }), and a filter's has to answer true or false. Its parameter takes the kind of the items unless you write one.The stack, the queue, the set and the map have none of these four. A stack and a queue exist to give things back in the order they went in, so sorting or shuffling one would undo what it is for. A set and a map have no order to put things in.
queue<T>, map<K,V> and set<T> each have one extra rule array<T>/list<T>/grid<T>/ stack<T> do not: their capacity must be a power of two - 2, 4, 8, 16, 32, and so on, never 10 or 30. This is not a GBML house rule; it comes straight from the hardware container each one compiles to in the Game Boy Advance module (a fixed-size double-ended queue for queue<T>, a fixed-size hash table for map<K,V> and for set<T>), each of which refuses to build against any other size. The untagged default, 8, already satisfies it, so this only ever comes up if you write your own /// @capacity N on one of these three and pick a number that is not a power of two - the compiler catches it and names the next legal size up, rather than letting the mistake reach devkitARM as a raw assertion three headers deep in a file you never wrote.
map<K,V> and set<T> can tell you whether one specific key or value is present, in roughly constant time, however many the container holds - that speed is the entire reason to reach for either over a searched list<T>. What neither offers is a way to walk every entry - there is no for (k, v in some_map)-shaped statement anywhere in GBML, on purpose: the language has no iterator concept at all today, and even if it did, an unordered container's own storage is keyed off each key's hash, not the order you inserted things in, so "the order you'd walk it in" is not even a well-defined idea to expose.
The documented way around it, when a project genuinely needs every entry: keep a parallel list<T> of everything you put in - a list<K> alongside a map<K,V>'s own keys, or a plain list<T> alongside a set<T>'s own members - updated every time you map_set/map_remove or set_add/set_remove, and walk that with an ordinary indexed for loop. It costs one more container and one more line at every write, in exchange for a real, orderly walk whenever you need one.
Structs in data containers covers the general pros, cons and how-to of putting a struct in any container. This section is narrower: the one place map<K,V> and set<T> genuinely part ways from every other container, worth stating plainly because it is easy to guess wrong:
map<K, ref<obj>> works. map<K, struct X> is stopped at the line that declares it. A map's value is never hashed, only stored, so a struct is not refused for the reason a struct key is. The trouble is somewhere else. GBML learns what a struct looks like from a real value written out in your code, such as [{ kind: 1, count: 3 }]. A map always starts empty ({}) and cannot be given a first entry in its declaration, so there is nothing for GBML to learn the shape from. The editor tells you so right away ("A map of structs cannot be built yet") instead of letting the game fail to build later. Make the map with the creation call and a struct as the value example (var stats = map_create(8, "", { hp: 0, speed: 0 })), which does show GBML the fields, or use a map<K, ref<obj>> and keep the fields on an instance, or one map per field with the same keys, or a list<struct X> you search.list<struct X>, stack<struct X> or queue<struct X> is stopped the same way when it starts empty (var drops = []; with a tag), and an array or grid too. Show it one real struct and it is fine. The shortest way is the creation call, whose example is a struct: var drops = list_create(8, { kind: 1, count: 3 }); is an empty list that GBML knows the fields of (stack_create and queue_create work the same). The older way works too: start it with one real struct in the declaration and then clear it (list_clear, stack_clear, queue_clear). The rule is not "no structs in containers". It is "GBML has to see one struct to learn the fields". A map is the one container that has no way to be shown one.set<struct X> is refused, at the line that declares it, and so is map<struct X, V> - a set's element is its own hash key, so the same rule that governs a map<K,V>'s key applies to it directly. There is no separate value to fall back on, and a struct GBML generates for you has no hash the Game Boy Advance module knows how to compute - a struct's own fields would have to decide its equality/hash (GBML has no == on a struct value at all yet), and a struct can itself contain a ref<obj> or a fractional number, a bigger question than this page's own scope.set<ref<obj>> and map<ref<obj>, V> both work. gbml::ref<T> (this project's own instance-reference type) has a real hash of its own, computed from the same two internal numbers that decide whether two references are equal in the first place - so a reference is exactly as usable as a key or a set element as an int or a string is. A reference that means "nothing" (nothing), or one whose instance has since been destroyed, is still a perfectly ordinary value to a map<K,V>/set<T> - it hashes and compares by what it is, never by whether the instance it once pointed at still exists.A hashable key or element is one of: int, real, bool, string, color, ref<obj>, or any of your project's own asset ids (sprite, sound, song, font, tileset, room, path, effect, background). If a project needs to track "have I already seen this struct," the honest answer today is a plain list<T> searched with list_contains - slower (it walks the list, set<T> does not have to), but it accepts any element type at all, struct included.
Every function on every container has its own page, linked from the table at the top. array<T> and list<T> are also covered as part of the language's own type system - see Variables, types and scope for how a container's type and capacity are declared, and Expressions and statements for how [i]/[# x, y] indexing actually reads.